package com.xls.aclservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xls.aclservice.entity.SysRole;
import com.xls.base.annotation.Log;
import com.xls.commonutils.Result;
import com.xls.aclservice.service.SysRoleService;
import com.xls.aclservice.vo.RoleQuery;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xls
 * @since 2021-03-23
 */
@RestController
@RequestMapping("/admin/acl/role")
@Api(value="角色信息接口")
@Slf4j
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Log(title = "角色管理", businessType = "查询")
    @PreAuthorize("hasAnyAuthority('system:sysRole:list')")
    @GetMapping("/getRole/{id}")
    public Result<SysRole> getRole(@PathVariable Long id) {
        return Result.success(sysRoleService.getRole(id));
    }

    @Log(title = "角色管理", businessType = "查询")
    @PreAuthorize("hasAnyAuthority('system:sysRole:list')")
    @PostMapping("/list/{page}/{limit}")
    public Result<Page<SysRole>> list(@PathVariable Integer page, @PathVariable Integer limit,
                                      @RequestBody RoleQuery roleQuery) {
        return Result.success(sysRoleService.getRolePage(page, limit, roleQuery));
    }

    @Log(title = "角色管理", businessType = "添加")
    @PreAuthorize("hasAnyAuthority('system:sysRole:save')")
    @PostMapping("/save")
    public Result<SysRole> save(@RequestBody SysRole sysRole) {
        sysRoleService.saveRole(sysRole);
        return Result.success(sysRole);
    }

    @Log(title = "角色管理", businessType = "修改")
    @PreAuthorize("hasAnyAuthority('system:sysRole:update')")
    @PostMapping("/update")
    public Result<SysRole> update(@RequestBody SysRole sysRole) {
        sysRoleService.saveRole(sysRole);
        return Result.success(sysRole);
    }

    @Log(title = "角色管理", businessType = "修改")
    @PreAuthorize("hasAnyAuthority('system:sysRole:update')")
    @PostMapping("/updateStatus")
    public Result<SysRole> updateStatus(@RequestBody SysRole sysRole) {
        sysRoleService.updateStatus(sysRole);
        return Result.success(sysRole);
    }

    @Log(title = "角色管理", businessType = "删除")
    @PreAuthorize("hasAnyAuthority('system:sysRole:remove')")
    @DeleteMapping("/remove/{id}")
    public Result<SysRole> remove(@PathVariable String id) {
        sysRoleService.removeRole(id);
        return Result.success();
    }

    /**
     * 获取角色下拉框
     * @return
     */
    @GetMapping("/getRoles")
    public Result<List<SysRole>> getRoles() {
        return Result.success(sysRoleService.list(new QueryWrapper<SysRole>().eq("status", true).orderByAsc("sort")));
    }

}

